﻿'******************************************************
'******************************************************
'Application Title: Google Widget Editor
'version: 1.0
'Date: 01-02-2008
'Requirements: win2k or later
'Author: Andrew Pye
'Author Email: StretchMasterP@googlemail.com
'License: Released under GNU GENERAL PUBLIC LICENSE v3
'ToDo:
'Changes:Imports System.Text
'******************************************************
'******************************************************

Public Class GGETreeView
    Public Event NodeSelected(ByRef Node As ClassContainer)
    Public Event NodeDoubleClicked(ByRef Node As ClassContainer)
    Private _widgetClass As Widget
    Public Property WidgetClass() As Widget
        Get
            Return _widgetClass
        End Get
        Set(ByVal value As Widget)
            _widgetClass = value
        End Set
    End Property

    'Public Function AddUpdateUserPref(ByVal Name As String, ByVal TUserPref As UserPref) As TreeNode
    '    Dim TNode As TreeNode = Nothing
    '    Dim ItemNode As TreeNode = Me.Nodes("UserPrefs")
    '    Try
    '        TNode = ItemNode.Nodes(Name)
    '        If TNode IsNot Nothing Then
    '            Dim TContainer As ClassContainer = TNode.Tag
    '            TContainer.ClassObject = TUserPref
    '        End If
    '    Catch ex As Exception
    '        'add item
    '        TNode = ItemNode.Nodes.Add(TUserPref.Name, "UserPref: " & TUserPref.Name)
    '        TNode.Tag = New ClassContainer(TUserPref, ClassContainer.enumClassType.UserPref)
    '    End Try
    '    Return TNode
    'End Function
    Private Sub CodeTree_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
        Dim TNode As TreeNode = Me.SelectedNode
        If TNode IsNot Nothing Then
            If TNode.Tag IsNot Nothing Then
                If TypeOf TNode.Tag Is ClassContainer Then
                    Me.OnNodeDoubleClicked(TNode.Tag)
                End If
            End If
        End If
    End Sub
    Private Sub CodeTree_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles Me.NodeMouseClick
        If e.Node.Tag IsNot Nothing Then
            If TypeOf e.Node.Tag Is ClassContainer Then
                OnNodeSelected(e.Node.Tag)
            End If
        End If

    End Sub
    Private Sub OnNodeDoubleClicked(ByRef Node As ClassContainer)
        RaiseEvent NodeDoubleClicked(Node)
    End Sub
    Private Sub OnNodeSelected(ByRef Node As ClassContainer)
        RaiseEvent NodeSelected(Node)
    End Sub
    Public Function AddNodeItem(ByVal oContainer As ClassContainer, Optional ByVal EnumParent As TreeNode = Nothing) As TreeNode
        '====================
        'See if new or update
        '====================
        Dim TNodeProject As TreeNode = Me.Nodes(0)
        Dim TSubNode As TreeNode = Nothing

        'Get the sub Node
        Select Case oContainer.ClassType
            Case ClassContainer.enumClassType.ModulePrefs
                TSubNode = TNodeProject.Nodes.Item("ModulePrefs")
            Case ClassContainer.enumClassType.Content
                TSubNode = TNodeProject.Nodes.Item("Content")
            Case ClassContainer.enumClassType.Enum
                'TSubNode = TNodeProject.Nodes.Item("Enum")
                TSubNode = EnumParent
            Case ClassContainer.enumClassType.Require
                TSubNode = TNodeProject.Nodes.Item("RequireLibs")
            Case ClassContainer.enumClassType.UserPref
                TSubNode = TNodeProject.Nodes.Item("ModulePrefs")
                TSubNode = TSubNode.Nodes.Item("UserPrefs")
            Case ClassContainer.enumClassType.CodeSnippet
                TSubNode = TNodeProject.Nodes.Item("CodeSnippets")
        End Select

        'get the item node
        Try

            'Add Item
            Dim TNode As TreeNode = TSubNode.Nodes.Add(oContainer.Name, oContainer.Name)
            TNode.Tag = oContainer
            If oContainer.ClassType = ClassContainer.enumClassType.UserPref Then
                TNode.ImageKey = "USER"
                TNode.SelectedImageKey = "USER"
            Else
                TNode.ImageKey = "MODULE"
                TNode.SelectedImageKey = "MODULE"
            End If
            Return TNode

        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Sub RefreshNodes()
        'Lock the window
        ModWindowUpdate.LockWindowUpdate(Me.Handle)
        Dim TSelNode As TreeNode = Me.SelectedNode
        If Me.Nodes.Count > 0 Then
            Me.Nodes(0).Text = Me.Nodes(0).Tag.Name
            RefreshNodes(Me.Nodes(0))
        End If
        Me.SelectedNode = TSelNode
        'Unlock the window
        ModWindowUpdate.LockWindowUpdate(0)
    End Sub
    Public Sub RefreshNode()
        'Lock the window
        ModWindowUpdate.LockWindowUpdate(Me.Handle)
        Dim TSelNode As TreeNode = Me.SelectedNode
        If TSelNode IsNot Nothing Then
            If Me.Nodes.Count > 0 Then
                If TSelNode.Tag IsNot Nothing Then
                    If TypeOf TSelNode.Tag Is ClassContainer Then
                        TSelNode.Text = TSelNode.Tag.Name
                    End If
                End If
            End If
        End If

        'Unlock the window
        ModWindowUpdate.LockWindowUpdate(0)
    End Sub

    Private Sub RefreshNodes(ByVal TNode As TreeNode)
        For Each Tobj As TreeNode In TNode.Nodes
            If Tobj.Tag IsNot Nothing Then
                If TypeOf Tobj.Tag Is ClassContainer Then
                    Tobj.Text = Tobj.Tag.Name
                    RefreshNodes(Tobj)
                End If
            End If
        Next
    End Sub
    Public Sub PopulateTree()


        '===============
        'Clear the Nodes
        '===============
        Me.Nodes.Clear()

        If WidgetClass IsNot Nothing Then

            '============
            'Project Node
            '============
            Dim TNodeProject As TreeNode = Me.Nodes.Add("PROJECT", WidgetClass.ProjectName)
            TNodeProject.Tag = New ClassContainer(Me.WidgetClass, ClassContainer.enumClassType.Application, False)
            TNodeProject.ImageKey = "APP"
            TNodeProject.SelectedImageKey = "APP"
            '====================
            'Attach the mod prefs
            '====================
            Dim TNodeModulePrefs As TreeNode = TNodeProject.Nodes.Add("ModulePrefs", "ModulePrefs")
            TNodeModulePrefs.Tag = New ClassContainer(Me.WidgetClass.ModulePrefs, ClassContainer.enumClassType.ModulePrefs, "ModulePrefs", False)
            TNodeModulePrefs.ImageKey = "OPEN_FOLDER"
            TNodeModulePrefs.SelectedImageKey = "OPEN_FOLDER"

            '=====================
            'Attach the User prefs
            '=====================
            Dim TNodeUserPrefs As TreeNode = TNodeModulePrefs.Nodes.Add("UserPrefs", "UserPrefs")
            TNodeUserPrefs.Tag = New ClassContainer(Me.WidgetClass.ModulePrefs.UserPrefs, ClassContainer.enumClassType.UserPrefs, "UserPrefs", False)
            TNodeUserPrefs.ImageKey = "OPEN_FOLDER"
            TNodeUserPrefs.SelectedImageKey = "OPEN_FOLDER"

            '=====================
            'Attach the oAuth
            '=====================
            Dim TNodeoAuth As TreeNode = TNodeModulePrefs.Nodes.Add("oAuth", "oAuth")
            TNodeoAuth.Tag = New ClassContainer(Me.WidgetClass.ModulePrefs.oAuth, ClassContainer.enumClassType.oAuth, "oAuth", False)
            TNodeoAuth.ImageKey = "OPEN_FOLDER"
            TNodeoAuth.SelectedImageKey = "OPEN_FOLDER"

            '======================
            'Loop through the items
            '======================
            For Each o_UserPref As UserPref In Me.WidgetClass.ModulePrefs.UserPrefs
                'Dim TUserPref As TreeNode = Me.AddUpdateUserPref(o_UserPref.Name, o_UserPref)
                Dim TUserPref As TreeNode = TNodeUserPrefs.Nodes.Add(o_UserPref.Name, o_UserPref.Name)
                TUserPref.Tag = New ClassContainer(o_UserPref, ClassContainer.enumClassType.UserPref, True)
                For Each o_Enum As EnumValue In o_UserPref.Enums
                    Dim TEnum As TreeNode = TUserPref.Nodes.Add(o_UserPref.Name & o_Enum.Value, o_Enum.Display_value)
                    TEnum.Tag = New ClassContainer(o_Enum, ClassContainer.enumClassType.Enum, True)
                    TEnum.ImageKey = "MODULE"
                    TEnum.SelectedImageKey = "MODULE"
                Next
            Next

            '============
            'Require Libs
            '============
            Dim TNodeRequireLibs As TreeNode = TNodeProject.Nodes.Add("RequireLibs", "RequireLibs")
            TNodeRequireLibs.ImageKey = "OPEN_FOLDER"
            TNodeRequireLibs.SelectedImageKey = "OPEN_FOLDER"
            TNodeRequireLibs.Tag = New ClassContainer(Me.WidgetClass.RequireLibs, ClassContainer.enumClassType.RequireLibs, "Require", False)

            For Each o_RequireLib As Require In Me.WidgetClass.RequireLibs
                Dim TRequireLib As TreeNode = TNodeRequireLibs.Nodes.Add(o_RequireLib.Feature)
                TRequireLib.Tag = New ClassContainer(o_RequireLib, ClassContainer.enumClassType.Require, True)
                TRequireLib.ImageKey = "MODULE"
                TRequireLib.SelectedImageKey = "MODULE"
            Next

            '============
            'Require Libs
            '============
            Dim TNodeCodeSnippets As TreeNode = TNodeProject.Nodes.Add("CodeSnippets", "CodeSnippets", "CodeSnippet")
            TNodeCodeSnippets.ImageKey = "OPEN_FOLDER"
            TNodeCodeSnippets.SelectedImageKey = "OPEN_FOLDER"
            TNodeCodeSnippets.Tag = New ClassContainer(Me.WidgetClass.Code_Snippets, ClassContainer.enumClassType.CodeSnippets, "CodeSnippet", False)
            For Each o_Code_Snippet As CodeSnippet In Me.WidgetClass.Code_Snippets
                Dim TCodeSnippet As TreeNode = TNodeCodeSnippets.Nodes.Add(o_Code_Snippet.Name)
                TCodeSnippet.Tag = New ClassContainer(o_Code_Snippet, ClassContainer.enumClassType.CodeSnippet, True)
                TCodeSnippet.ImageKey = "MODULE"
                TCodeSnippet.SelectedImageKey = "MODULE"
            Next

            '=======
            'contect
            '=======
            Dim TNodeContent As TreeNode = TNodeProject.Nodes.Add("Content", "Content", "Content")
            TNodeContent.ImageKey = "OPEN_FOLDER"
            TNodeContent.SelectedImageKey = "OPEN_FOLDER"
            TNodeContent.Tag = New ClassContainer(Me.WidgetClass.Content, ClassContainer.enumClassType.Contents, "Content", False)
            For Each o_Content As Content In Me.WidgetClass.Content
                Dim TContent As TreeNode = TNodeContent.Nodes.Add(o_Content.Name)
                TContent.Tag = New ClassContainer(o_Content, ClassContainer.enumClassType.Content, True)
                TContent.ImageKey = "MODULE"
                TContent.SelectedImageKey = "MODULE"
            Next
            'Me.ExpandAll()
            Me.Nodes(0).Expand()
        End If
    End Sub
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.ImageList = Me.ImageList1
    End Sub
End Class
